//
//  YLManualCheckSearchViewController.m
//  TheTenthAnniversaryOfMobileUnion
//
//  Created by kuangbiao on 2019/1/24.
//  Copyright © 2019 kuangbiao. All rights reserved.
//

#import "YLManualCheckSearchViewController.h"
#import "YLManualCheckSearchCell.h"
#import "Header.h"
#import "YLSearchUserModel.h"
#import "YLSignSuccessViewController.h"
#import "YLSignFailedViewController.h"
#import "YLSettingViewController.h"

@interface YLManualCheckSearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *searchBacView;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (weak, nonatomic) IBOutlet UITextField *usernameTxt;
@property (weak, nonatomic) IBOutlet UIButton *signBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *nodataLabel;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSIndexPath *selectPath;
@end

@implementation YLManualCheckSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView{
    self.navigationItem.title = @"人工签到";
    self.searchBacView.layer.borderColor = [UIColor colorWithHexString:@"C7C7C7"].CGColor;
    self.searchBacView.layer.borderWidth = 1.0f;
    self.searchBacView.layer.cornerRadius = 5.0f;
    self.searchBtn.layer.cornerRadius = 15.0f;
    self.signBtn.layer.cornerRadius = 30.0f;
    self.signBtn.hidden = YES;
    self.nodataLabel.hidden = YES;
    [self.collectionView registerNib:[UINib nibWithNibName:@"YLManualCheckSearchCell" bundle:nil] forCellWithReuseIdentifier:@"YLManualCheckSearchCell"];
    _selectPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self createLeftBarItem];
}

- (void)createLeftBarItem{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"arrows black"] forState:UIControlStateNormal];
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [leftBtn addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}
#pragma mark - 查询按钮点击事件
extern NSString *kServerHttpBaseUrl;
- (IBAction)searchBtnClickAction:(id)sender {
    
    [self.view endEditing:YES];
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerHttpBaseUrl,kSceneHandQuey_url];
    NSDictionary *params = @{@"realName":self.usernameTxt.text?:@""};
    [self.dataSource removeAllObjects];
    [HttpRequest requestWithURLString:url parameters:params type:HttpRequestTypePost success:^(id responseObject) {
        NSDictionary *dict = responseObject;
        YLHttpModel *httpModel = [YLHttpModel yy_modelWithDictionary:dict];
        if([httpModel.status isEqualToString:@"200"]){
            [self.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YLSearchUserModel class] json:[dict objectForKey:@"data"]]];
        }else{
            [KDAlertView alertWithMessage:httpModel.msg?:@""];
        }
        if(self.dataSource.count>0){
            self.nodataLabel.hidden = YES;
            self.signBtn.hidden = NO;
        }else{
            self.nodataLabel.hidden = NO;
            self.signBtn.hidden = YES;
        }
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        [KDLoadFrame withState:KDLoadStateError];
    }];
}
#pragma mark - 签到按钮点击事件
- (IBAction)signBtnClickAction:(id)sender {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerHttpBaseUrl,kUserSign_url];
    YLSearchUserModel *searchModel = self.dataSource[self.selectPath.item];
    NSDictionary *params = @{@"userNo":searchModel.userNo?:@"",@"meetNo":[CMBasicData getMerInfo].meetingNo?:@"",@"signType":@"sceneHand"};
    [self.dataSource removeAllObjects];
    [HttpRequest requestWithURLString:url parameters:params type:HttpRequestTypePost success:^(id responseObject) {
        NSDictionary *dict = responseObject;
        YLHttpModel *httpModel = [YLHttpModel yy_modelWithDictionary:dict];
        [KDAlertView alertWithMessage:httpModel.msg?:@""];
        if([httpModel.status isEqualToString:@"200"]){
            [self.collectionView reloadData];
            self.signBtn.hidden = YES;
            self.usernameTxt.text = @"";
        }else{
            
        }
    } failure:^(NSError *error) {
        [KDLoadFrame withState:KDLoadStateError];
    }];
}

- (void)popView{
    
    for (UIViewController *vc in self.navigationController.viewControllers){
        if([vc isKindOfClass:[YLSignFailedViewController class]]){
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] - 5)] animated:YES];
            return;
        }
        if([vc isKindOfClass:[YLSettingViewController class]]){
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] - 3)] animated:YES];
            return ;
        }
    }
    
}

#pragma mark --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YLManualCheckSearchCell *cell = (YLManualCheckSearchCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YLManualCheckSearchCell" forIndexPath:indexPath];
    if(self.dataSource.count>0){
        cell.userModel = self.dataSource[indexPath.row];
        if (_selectPath == indexPath) {
             cell.selectImageView.hidden = NO;
        }else{
             cell.selectImageView.hidden = YES;
        }
       
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake((540 - 120)/2.0, 260);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 40, 0, 40);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YLManualCheckSearchCell *cell = (YLManualCheckSearchCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.selectImageView.hidden = NO;
    
    //根据idenxPath获取对应的cell
    int newRow = (int)[indexPath row];
    int oldRow = (int)(_selectPath != nil) ? (int)[_selectPath row]:-1;
    if (newRow != oldRow) {
        YLManualCheckSearchCell *newCell =  (YLManualCheckSearchCell *)[collectionView cellForItemAtIndexPath:indexPath];
        newCell.selectImageView.hidden = NO;
        
        YLManualCheckSearchCell *oldCell = (YLManualCheckSearchCell *)[collectionView cellForItemAtIndexPath:_selectPath];
        oldCell.selectImageView.hidden = YES;
        _selectPath = [indexPath copy];
    }
}

- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

@end
